###Models for final Version of the Paper
library(scales)
library(stargazer)
library(rockchalk)
library(effects)
require(ggplot2)
require(pscl)
require(MASS)
require(boot)
library(rio)
library(standardize)
library(car)
library(readstata13)
library(nnet)


## Read in the data
d1 = read.dta13("replication-dataset_public.dta")

d1$Compensatef = factor(d1$ncompensate)
d1$Ncompensate=relevel(d1$Compensatef, ref=c("0"))
d1$trustee=d1$representation_1
d1$libcon[d1$libcon<1]=NA
d1$party[d1$party==0] = NA
d1$party[d1$party==-99] = NA
d1$partyid = factor(d1$party, labels = c("Democrat", "Republican", "Independent", "Other"))
d1$partyid=relevel(d1$partyid, ref="Independent")
##Clean some variables
d1$termlimits[d1$termlimits>1]=NA
##Subset the data to always responders
d1a <- d1[ which(d1$responded_always==1),]
##Create a dataset so we only have one response per person. 
d2 <- d1[ which(d1$topic==c("Voter Registration")),]
d2a <- d1a[ which(d1a$topic==c("Development")),]
d2a$dcomp=0
d2a$dcomp[d2a$Compensate>0]=1

##Table 1
mod.18.s=lm(trustee~open_s+consc_s+extra_s+agree_s+stable_s, data=d2)
summary(mod.18.s)
##Use more controls
mod.18.i=lm(trustee~open_s+consc_s+extra_s+agree_s+stable_s+libcon+female+termlimits+closevote+tenure+logpop, data=d2)
stargazer(mod.18.s,mod.18.i, type="text")
export_summs(mod.18.s,mod.18.i, robust="HC1", confint = TRUE, digits = 3,to.file = "docx", file.name = "Table1.docx")


##Robust Standard Errors + Export to file
mod.18.b=lm(trustee~open_s+consc_s+extra_s+agree_s+stable_s+closevote+tenure+logpop+libcon, data=d2)
summ(mod.18.b,robust="HC1", confint = TRUE, digits = 3)
mod.18.i=lm(trustee~open_s+consc_s+extra_s+agree_s+stable_s+tenure+libcon+open_s:libcon+agree_s:libcon, data=d2)
##mean centered coefficients
summ(mod.18.i, scale=TRUE,robust="HC1", confint = TRUE, digits = 3)
##Not mean centered
summ(mod.18.i, robust="HC1", confint = TRUE, digits = 3)
mod.18.s=lm(trustee~open_s+consc_s+extra_s+agree_s+stable_s,data=d2)
export_summs(mod.18.s,mod.18.b,mod.18.i, robust="HC1", confint = TRUE, digits = 3,to.file = "docx", file.name = "Table1.docx")

##Response Rates Table 3
mod.20=glm(R~EmailAgrees+open_s+consc_s+extra_s+agree_s+stable_s+libcon+female+termlimits+closevote+tenure+logpop, data=d1, family='binomial')
mod.21=glm(R~EmailAgrees+open_s*EmailAgrees+consc_s*EmailAgrees+extra_s*EmailAgrees+agree_s*EmailAgrees+stable_s*EmailAgrees+libcon+female+termlimits+closevote+tenure+logpop, data=d1, family='binomial')
export_summs(mod.20,mod.21, robust="HC1", confint = TRUE, digits = 3,to.file = "docx", file.name = "Table3(Appendix).docx")
stargazer(mod.20,mod.21, type="text")
##Plot model results
interactions::interact_plot(mod.21, pred = agree_s, modx= EmailAgrees,interval = TRUE, plot.points = F)+
  labs(y="Probability of Responding", title="",x="Agreeableness")

##Compensating Table 4 binomial
##Compensating robustness check. Compensate coded -1,0,1 Final Model for Table 4
mod.111c=polr(Compensatef~EmailAgrees+libcon+female+termlimits+closevote+tenure+logpop, data=d1a, method=c("logistic"))
mod.11c=polr(Compensatef~EmailAgrees+open_s+consc_s+extra_s+agree_s+stable_s+libcon+female+termlimits+closevote+tenure+logpop, data=d1a, method=c("logistic"))
mod.11ic=polr(Compensatef~EmailAgrees+open_s+consc_s+extra_s+agree_s+stable_s+libcon+female+termlimits+closevote+tenure+logpop+EmailAgrees:open_s+EmailAgrees:consc_s+EmailAgrees:extra_s+EmailAgrees:agree_s+EmailAgrees:stable_s, data=d1a, method=c("logistic"))
stargazer(mod.111c,mod.11c,mod.11ic, type='text')
export_summs(mod.111c,mod.11c,mod.11ic, robust="HC1", confint = TRUE, digits = 3,to.file = "docx", file.name = "Table4 (Appendix).docx")
allEffects(mod.11ic)
#binomial Model logit
mod.111=glm(dcomp~EmailAgrees+libcon+female+termlimits+closevote+tenure+logpop, data=d2a, family="binomial")
mod.11=glm(dcomp~EmailAgrees+open_s+consc_s+extra_s+agree_s+stable_s+libcon+female+termlimits+closevote+tenure+logpop, data=d2a, family="binomial")
mod.11i=glm(dcomp~EmailAgrees+open_s+consc_s+extra_s+agree_s+stable_s+libcon+female+termlimits+closevote+tenure+logpop+EmailAgrees:open_s+EmailAgrees:consc_s+EmailAgrees:extra_s+EmailAgrees:agree_s+EmailAgrees:stable_s, data=d2a, family="binomial")
export_summs(mod.111,mod.11,mod.11i, robust="HC1", confint = TRUE, digits = 3,to.file = "docx", file.name = "Table4.docx")
allEffects(mod.11i)
stargazer(mod.111,mod.11,mod.11i, type='text')

##Plot model results
jtools::effect_plot(mod.11, pred = agree_s, interval = TRUE, plot.points = TRUE)+
  labs(y="Probability of Compensating", title="", 
       x="Agreeableness")

interactions::interact_plot(mod.11i, pred = agree_s, modx= EmailAgrees,interval = TRUE, plot.points = F)+
  labs(y="Probability of Compensating", title="",x="Agreeableness")


##Compensating robustness check. Compensate coded -1,0,1 Final Model for Table 4 Multinomial Models
mod.111c=multinom(Ncompensate~EmailAgrees+closevote+tenure+logpop, data=d1)
mod.11c=multinom(Ncompensate~EmailAgrees+open_s+consc_s+extra_s+agree_s+stable_s+closevote+tenure+logpop, data=d1)
mod.11ic=multinom(Ncompensate~EmailAgrees+open_s+consc_s+extra_s+agree_s+stable_s+closevote+tenure+logpop+EmailAgrees:open_s+EmailAgrees:consc_s+EmailAgrees:extra_s+EmailAgrees:agree_s+EmailAgrees:stable_s, data=d1)
stargazer(mod.111c,mod.11c,mod.11ic, type='text')
##Percentage who compensate
prop.table(table(nd1$Compensatef))
##Subset to agree vs disagree email
d3 <- d1[ which(d1$EmailAgrees==c("Yes")),]
d4 <- d1[ which(d1$EmailAgrees==c("No")),]
prop.table(table(d3$Compensatef))
prop.table(table(d4$Compensatef))




